package com.pwc.conversionTool.csv;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.dom4j.DocumentException;
import org.springframework.web.multipart.MultipartFile;

import com.pwc.conversionTool.IO.Writer;
import com.pwc.conversionTool.Manager.XMLConfigImpl;

public class CsvUpload {
	
	private ServletContext context;
	
	public CsvUpload(ServletContext context)
	{
		this.context=context;
	}
	
	/**
	 * upload to 
	 * @param file
	 * @param fileName
	 * @throws DocumentException
	 * @throws IOException
	 */
	public  void uploadSingleFile(MultipartFile file,  String fileName,  String uploadPath) throws DocumentException, IOException{

		File folderFile=new File(uploadPath);
 	   
        byte[] bytes = file.getBytes();
        
        String FileName=fileName.equals("")?file.getOriginalFilename():fileName;
        
        String path=folderFile.getPath()+File.separator+FileName;
        
        File saveFile=new File(path);
        if(FileName.length()>0){
        	 if(!saveFile.exists())
             {
             	Writer.writeByteArrayToFile(saveFile, bytes);
             }else {
          	   saveFile=new File(folderFile.getPath()+File.separator+System.currentTimeMillis()+FileName);
          	   Writer.writeByteArrayToFile(saveFile, bytes);
             }
        }
	}
	
	
	public  void uploadSingleFile(MultipartFile file,  String uploadPath) throws DocumentException, IOException
	{
		 uploadSingleFile( file,  "", uploadPath);
	}
	
	
	/**
	 * upload to 
	 * @param file
	 * @param fileName
	 * @throws DocumentException
	 * @throws IOException
	 */
	public  void uploadMutiFile(MultipartFile[] files,  String uploadPath) throws DocumentException, IOException{
		for (MultipartFile multipartFile : files) {
			uploadSingleFile(multipartFile, uploadPath);
		}
	}

}
